/**
 * @param {number[]} nums
 * @param  {number} target
 * @return {number[]}
 */
/*const twosum = function(){
  // 用空间换时间 减少一重循环
    const diffs = {};// key val
    const len = nums.length ;
    for( let i = 0; i < len ; i++){
        if(diffs[target-nums[i]] != undefined){
            return [diffs[target-nums[i]], i]
        }
        else {
           diffs[nums[i]] = i;
        }
    }
}*/
var twoSum = function(nums, target) {
    let map = new Map();
    let n = nums.length;
    for(let i = 0 ;i < n ; i++){
        if(map.has(target-nums[i]))
        return [map.get(target-nums[i]),i]
        else{
            map.set(nums[i],i);
        }
    }
    return null;
}
console.log(twoSum([2,0,11,15,7],9));